﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WikcLib.Config
{
    public class Configuration
    {
        public static Boolean SaveConfigurationInfo(string strKey, string strValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
                XmlNode xNode;
                XmlElement xElemUID;
                xNode = xDoc.SelectSingleNode("//appSettings");


                xElemUID = (XmlElement)xNode.SelectSingleNode("//add[@key='" + strKey + "']");
                xElemUID.SetAttribute("value", strValue);

                
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 保存信息到web.config
        /// </summary>
        /// <returns></returns>
        public static Boolean SaveEmailInfo(string strServers, string strPort, string strEmail, string strUserName, string strPassWord, string strDisplayName)
        {
            try
            { 
                SaveConfigurationInfo("Servers", strServers); 
                SaveConfigurationInfo("Port", strPort); 
                SaveConfigurationInfo("Email", strEmail);
                SaveConfigurationInfo("UserName", strUserName);
                SaveConfigurationInfo("PassWord", strPassWord);
                SaveConfigurationInfo("DisplayName", strDisplayName);  
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
